﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Globalization;
namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类ProductDAO。
    /// </summary>
    public class ProductDAO
    {
        public ProductDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ProductID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
            parameters[0].Value = ProductID;

            int result = DbHelperSQL.RunProcedure("UP_Product_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public int getIDByNo(string no)
        {
            string sql = "select ProductID from [Product] where ItemCode='{0}'";
         return Convert.ToInt32(DbHelperSQL.GetData(string.Format(sql,no)));
        }
        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(TimKen.DataEntity.Product model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@ItemCode", SqlDbType.NChar,20),
					new SqlParameter("@ItemClass", SqlDbType.NChar,10),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@InvQty", SqlDbType.Float,8),
					new SqlParameter("@BookedQty", SqlDbType.Float,8),
					new SqlParameter("@Image", SqlDbType.VarChar,64),
					new SqlParameter("@Currency", SqlDbType.NChar,5),
					new SqlParameter("@BasicPrice", SqlDbType.Decimal,9),
					new SqlParameter("@IsPromote", SqlDbType.Bit,1),
					new SqlParameter("@PromoteDiscount", SqlDbType.Decimal,9),
					new SqlParameter("@PromoteQty", SqlDbType.Float,8),
					new SqlParameter("@TaxClassID", SqlDbType.Int,4),
					new SqlParameter("@TaxRate", SqlDbType.Float,8),
					new SqlParameter("@BrandID", SqlDbType.Int,4),
					new SqlParameter("@BaseUnit", SqlDbType.NChar,20),
					new SqlParameter("@SalesUnit", SqlDbType.NChar,20),
					new SqlParameter("@Vaild", SqlDbType.Bit,1),
					new SqlParameter("@ValidFrom", SqlDbType.DateTime),
					new SqlParameter("@VailTo", SqlDbType.DateTime),
					new SqlParameter("@DateAdded", SqlDbType.DateTime),
					new SqlParameter("@LastModified", SqlDbType.DateTime),
					new SqlParameter("@Ordered", SqlDbType.Int,4),
                    new SqlParameter("@Weight",SqlDbType.Float),
   					new SqlParameter("@PromoteFrom",SqlDbType.DateTime),
					new SqlParameter("@PromoteTo",SqlDbType.DateTime),
					new SqlParameter("@IntroDocFiles",SqlDbType.VarChar,64),
					new SqlParameter("@ShipDays",SqlDbType.Int,4),
                   	new SqlParameter("@Freeze", SqlDbType.Bit,1),
					new SqlParameter("@FreezeFrom", SqlDbType.DateTime),
					new SqlParameter("@FreezeTo", SqlDbType.DateTime)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ItemCode;
            parameters[2].Value = model.ItemClass;
            parameters[3].Value = model.CategoryID;
            parameters[4].Value = model.InvQty;
            parameters[5].Value = model.BookedQty;
            //parameters[6].Value = model.Image;
            parameters[7].Value = model.Currency;
            parameters[8].Value = model.BasicPrice;
            parameters[9].Value = model.IsPromote;
            parameters[10].Value = model.PromoteDiscount;
            parameters[11].Value = model.PromoteQty;
            parameters[12].Value = model.TaxClassID;
            parameters[13].Value = model.TaxRate;
            parameters[14].Value = model.BrandID;
            parameters[15].Value = model.BaseUnit;
            parameters[16].Value = model.SalesUnit;
            parameters[17].Value = model.Vaild;
            parameters[18].Value = model.ValidFrom;
            parameters[19].Value = model.VailTo;
            parameters[20].Value = model.DateAdded;
            parameters[21].Value = model.LastModified;
            parameters[22].Value = model.Ordered;
            parameters[23].Value = model.Weight;
            parameters[24].Value = model.PromoteFrom;
            parameters[25].Value = model.PromoteTo;
            parameters[26].Value = model.IntroDocFiles;
            parameters[27].Value = model.ShipDays;
            parameters[28].Value = model.Freeze;
            parameters[29].Value = model.FreezeFrom;
            parameters[30].Value = model.FreezeTo;
            DbHelperSQL.RunProcedure("UP_Product_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }


        public void UpdatePromote(int ProductID, bool IsPromote, decimal? PromoteDiscount, DateTime? PromoteFrom, DateTime? PromoteTo)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@IsPromote", SqlDbType.NChar,1),
                    new SqlParameter("@PromoteDiscount", SqlDbType.Decimal,9),
					new SqlParameter("@PromoteFrom",SqlDbType.DateTime),
					new SqlParameter("@PromoteTo",SqlDbType.DateTime)};

            parameters[0].Value = ProductID;



            parameters[1].Value = IsPromote ? 1 : 0;
            parameters[2].Value = PromoteDiscount;
            parameters[3].Value = PromoteFrom;
            parameters[4].Value = PromoteTo;
            DbHelperSQL.RunProcedure("UP_Product_UpdatePromote", parameters);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Product model)
        {

            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@ItemCode", SqlDbType.NChar,20),
					new SqlParameter("@ItemClass", SqlDbType.NChar,10),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@InvQty", SqlDbType.Float,8),
					new SqlParameter("@BookedQty", SqlDbType.Float,8),
					new SqlParameter("@Image", SqlDbType.VarChar,64),
					new SqlParameter("@Currency", SqlDbType.NChar,5),
					new SqlParameter("@BasicPrice", SqlDbType.Decimal,9),
					new SqlParameter("@IsPromote", SqlDbType.Bit,1),
					new SqlParameter("@PromoteDiscount", SqlDbType.Decimal,9),
					new SqlParameter("@PromoteQty", SqlDbType.Float,8),
					new SqlParameter("@TaxClassID", SqlDbType.Int,4),
					new SqlParameter("@TaxRate", SqlDbType.Float,8),
					new SqlParameter("@BrandID", SqlDbType.Int,4),
					new SqlParameter("@BaseUnit", SqlDbType.NChar,20),
					new SqlParameter("@SalesUnit", SqlDbType.NChar,20),
					new SqlParameter("@Vaild", SqlDbType.Bit,1),
					new SqlParameter("@ValidFrom", SqlDbType.DateTime),
					new SqlParameter("@VailTo", SqlDbType.DateTime),
					new SqlParameter("@DateAdded", SqlDbType.DateTime),
					new SqlParameter("@LastModified", SqlDbType.DateTime),
					new SqlParameter("@Ordered", SqlDbType.Int,4),
					new SqlParameter("@Weight",SqlDbType.Decimal,9),
                    new SqlParameter("@WeightUnit",SqlDbType.NVarChar,20),
					new SqlParameter("@PromoteFrom",SqlDbType.DateTime),
					new SqlParameter("@PromoteTo",SqlDbType.DateTime),
					new SqlParameter("@IntroDocFiles",SqlDbType.VarChar,64),
					new SqlParameter("@ShipDays",SqlDbType.Int,4)	,
                    new SqlParameter("@Freeze", SqlDbType.Bit,1),
					new SqlParameter("@FreezeFrom", SqlDbType.DateTime),
					new SqlParameter("@FreezeTo", SqlDbType.DateTime),
                    new SqlParameter("@IsDisplay", SqlDbType.Bit,1),
                    new SqlParameter("@IsRecommend", SqlDbType.Bit,1),                    };
            parameters[0].Value = model.ProductID;
            parameters[1].Value = model.ItemCode;
            parameters[2].Value = model.ItemClass;
            parameters[3].Value = model.CategoryID;
            parameters[4].Value = model.InvQty;
            parameters[5].Value = model.BookedQty;
            parameters[6].Value = model.Image;
            parameters[7].Value = model.Currency;
            parameters[8].Value = model.BasicPrice;
            parameters[9].Value = model.IsPromote;
            parameters[10].Value = model.PromoteDiscount;
            parameters[11].Value = model.PromoteQty;
            parameters[12].Value = model.TaxClassID;
            parameters[13].Value = model.TaxRate;
            parameters[14].Value = model.BrandID;
            parameters[15].Value = model.BaseUnit;
            parameters[16].Value = model.SalesUnit;
            parameters[17].Value = model.Vaild;
            parameters[18].Value = model.ValidFrom;
            parameters[19].Value = model.VailTo;
            parameters[20].Value = model.DateAdded;
            parameters[21].Value = model.LastModified;
            parameters[22].Value = model.Ordered;
            parameters[23].Value = model.Weight;
            parameters[24].Value = model.WeightUnit;
            parameters[25].Value = model.PromoteFrom;
            parameters[26].Value = model.PromoteTo;
            parameters[27].Value = model.IntroDocFiles;
            parameters[28].Value = model.ShipDays;
            parameters[29].Value = model.Freeze;
            parameters[30].Value = model.FreezeFrom;
            parameters[31].Value = model.FreezeTo;
            parameters[32].Value = model.IsDisplay;
            parameters[33].Value = model.IsRecommend;
            DbHelperSQL.RunProcedure("UP_Product_Update", parameters, out rowsAffected);
        }

        public void UploadImg(int ProductID, string impName)
        {
            SqlParameter[] parameters = { new SqlParameter("@Image", SqlDbType.VarChar, 64) };
            parameters[0].Value = impName;
            DbHelperSQL.ExecuteSql("UPDATE Product set [Image]=@Image where ProductID=" + ProductID, parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ProductID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
            parameters[0].Value = ProductID;

            DbHelperSQL.RunProcedure("UP_Product_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Product GetModel(int ProductID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ProductID", SqlDbType.Int,4)};
            parameters[0].Value = ProductID;

            TimKen.DataEntity.Product model = new TimKen.DataEntity.Product();
            DataSet ds = DbHelperSQL.RunProcedure("UP_Product_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                model.ItemCode = ds.Tables[0].Rows[0]["ItemCode"].ToString();
                model.ItemClass = ds.Tables[0].Rows[0]["ItemClass"].ToString();
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "")
                {
                    model.CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InvQty"].ToString() != "")
                {
                    model.InvQty = decimal.Parse(ds.Tables[0].Rows[0]["InvQty"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BookedQty"].ToString() != "")
                {
                    model.BookedQty = decimal.Parse(ds.Tables[0].Rows[0]["BookedQty"].ToString());
                }
                 model.Image = ds.Tables[0].Rows[0]["Image"].ToString();
                model.Currency = ds.Tables[0].Rows[0]["Currency"].ToString();
                if (ds.Tables[0].Rows[0]["BasicPrice"].ToString() != "")
                {
                    model.BasicPrice = decimal.Parse(ds.Tables[0].Rows[0]["BasicPrice"].ToString());
                }
                model.IsPromote = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsPromote"]);
                if (ds.Tables[0].Rows[0]["PromoteDiscount"].ToString() != "")
                {
                    model.PromoteDiscount = decimal.Parse(ds.Tables[0].Rows[0]["PromoteDiscount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PromoteQty"].ToString() != "")
                {
                    model.PromoteQty = decimal.Parse(ds.Tables[0].Rows[0]["PromoteQty"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaxClassID"].ToString() != "")
                {
                    model.TaxClassID = int.Parse(ds.Tables[0].Rows[0]["TaxClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaxRate"].ToString() != "")
                {
                    model.TaxRate = decimal.Parse(ds.Tables[0].Rows[0]["TaxRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BrandID"].ToString() != "")
                {
                    model.BrandID = int.Parse(ds.Tables[0].Rows[0]["BrandID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DiscountGroupID"].ToString() != "")
                {
                    model.DiscountGroupID = int.Parse(ds.Tables[0].Rows[0]["DiscountGroupID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductLineID"].ToString() != "")
                {
                    model.ProductLineID = int.Parse(ds.Tables[0].Rows[0]["ProductLineID"].ToString());
                }
                model.BaseUnit = ds.Tables[0].Rows[0]["BaseUnit"].ToString();
                model.SalesUnit = ds.Tables[0].Rows[0]["SalesUnit"].ToString();
                if (ds.Tables[0].Rows[0]["Vaild"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Vaild"].ToString() == "1") || (ds.Tables[0].Rows[0]["Vaild"].ToString().ToLower() == "true"))
                    {
                        model.Vaild = true;
                    }
                    else
                    {
                        model.Vaild = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ValidFrom"].ToString() != "")
                {
                    model.ValidFrom = DateTime.Parse(ds.Tables[0].Rows[0]["ValidFrom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["VailTo"].ToString() != "")
                {
                    model.VailTo = DateTime.Parse(ds.Tables[0].Rows[0]["VailTo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DateAdded"].ToString() != "")
                {
                    model.DateAdded = DateTime.Parse(ds.Tables[0].Rows[0]["DateAdded"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastModified"].ToString() != "")
                {
                    //model.LastModified = DateTime.Parse(ds.Tables[0].Rows[0]["LastModified"].ToString());
                    model.LastModified = Convert.ToDateTime(ds.Tables[0].Rows[0]["LastModified"]);
                }
                if (ds.Tables[0].Rows[0]["Ordered"].ToString() != "")
                {
                    model.Ordered = int.Parse(ds.Tables[0].Rows[0]["Ordered"].ToString());    
                }

                if (ds.Tables[0].Rows[0]["Weight"].ToString() != "")
                {
                    //model.Weight = decimal.Parse(ds.Tables[0].Rows[0]["Weight"].ToString());
                    model.Weight  = Decimal.Parse(ds.Tables[0].Rows[0]["Weight"].ToString(), NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
                }
                //添加促销时间、技术文件名、备货时效

                if (ds.Tables[0].Rows[0]["PromoteFrom"].ToString() != "")
                {
                    model.PromoteFrom = DateTime.Parse(ds.Tables[0].Rows[0]["PromoteFrom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PromoteTo"].ToString() != "")
                {
                    model.PromoteTo = DateTime.Parse(ds.Tables[0].Rows[0]["PromoteTo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IntroDocFiles"].ToString() != "")
                {
                    model.IntroDocFiles = ds.Tables[0].Rows[0]["IntroDocFiles"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ShipDays"].ToString() != "")
                {
                    model.ShipDays = int.Parse(ds.Tables[0].Rows[0]["ShipDays"].ToString());
                }
                model.WeightUnit = ds.Tables[0].Rows[0]["WeightUnit"].ToString();
                if (ds.Tables[0].Rows[0]["Freeze"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Freeze"].ToString() == "1") || (ds.Tables[0].Rows[0]["Freeze"].ToString().ToLower() == "true"))
                    {
                        model.Freeze = true;
                    }
                    else
                    {
                        model.Freeze = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["FreezeFrom"].ToString() != "")
                {
                    model.FreezeFrom = DateTime.Parse(ds.Tables[0].Rows[0]["FreezeFrom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FreezeTo"].ToString() != "")
                {
                    model.FreezeTo = DateTime.Parse(ds.Tables[0].Rows[0]["FreezeTo"].ToString());
                }


                if (ds.Tables[0].Rows[0]["MinNumber"].ToString() != "")
                {
                    model.MinNumber = int.Parse(ds.Tables[0].Rows[0]["MinNumber"].ToString());
                }

                if (ds.Tables[0].Rows[0]["MinMultiple"].ToString() != "")
                {
                    model.MinMultiple = int.Parse(ds.Tables[0].Rows[0]["MinMultiple"].ToString());
                }

                if (ds.Tables[0].Rows[0]["IsRecommend"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsRecommend"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsRecommend"].ToString().ToLower() == "true"))
                    {
                        model.IsRecommend = true;
                    }
                    else
                    {
                        model.IsRecommend = false;
                    }
                }

                return model;
            }
            else
            {
                return null;
            }
        }



        public TimKen.DataEntity.Product GetModel(string itemCode)
        {
          

            TimKen.DataEntity.Product model = new TimKen.DataEntity.Product();

            string sql = string.Format("SELECT * FROM Product WHERE ItemCode = '{0}'",itemCode.Trim());

            DataSet ds = DbHelperSQL.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                model.ItemCode = ds.Tables[0].Rows[0]["ItemCode"].ToString();
                model.ItemClass = ds.Tables[0].Rows[0]["ItemClass"].ToString();
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "")
                {
                    model.CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InvQty"].ToString() != "")
                {
                    model.InvQty = decimal.Parse(ds.Tables[0].Rows[0]["InvQty"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BookedQty"].ToString() != "")
                {
                    model.BookedQty = decimal.Parse(ds.Tables[0].Rows[0]["BookedQty"].ToString());
                }
                //model.Image = ds.Tables[0].Rows[0]["Image"].ToString();
                model.Currency = ds.Tables[0].Rows[0]["Currency"].ToString();
                if (ds.Tables[0].Rows[0]["BasicPrice"].ToString() != "")
                {
                    model.BasicPrice = decimal.Parse(ds.Tables[0].Rows[0]["BasicPrice"].ToString());
                }
                model.IsPromote = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsPromote"]);
                if (ds.Tables[0].Rows[0]["PromoteDiscount"].ToString() != "")
                {
                    model.PromoteDiscount = decimal.Parse(ds.Tables[0].Rows[0]["PromoteDiscount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PromoteQty"].ToString() != "")
                {
                    model.PromoteQty = decimal.Parse(ds.Tables[0].Rows[0]["PromoteQty"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaxClassID"].ToString() != "")
                {
                    model.TaxClassID = int.Parse(ds.Tables[0].Rows[0]["TaxClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaxRate"].ToString() != "")
                {
                    model.TaxRate = decimal.Parse(ds.Tables[0].Rows[0]["TaxRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BrandID"].ToString() != "")
                {
                    model.BrandID = int.Parse(ds.Tables[0].Rows[0]["BrandID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DiscountGroupID"].ToString() != "")
                {
                    model.DiscountGroupID = int.Parse(ds.Tables[0].Rows[0]["DiscountGroupID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductLineID"].ToString() != "")
                {
                    model.ProductLineID = int.Parse(ds.Tables[0].Rows[0]["ProductLineID"].ToString());
                }
                model.BaseUnit = ds.Tables[0].Rows[0]["BaseUnit"].ToString();
                model.SalesUnit = ds.Tables[0].Rows[0]["SalesUnit"].ToString();
                if (ds.Tables[0].Rows[0]["Vaild"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Vaild"].ToString() == "1") || (ds.Tables[0].Rows[0]["Vaild"].ToString().ToLower() == "true"))
                    {
                        model.Vaild = true;
                    }
                    else
                    {
                        model.Vaild = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ValidFrom"].ToString() != "")
                {
                    model.ValidFrom = DateTime.Parse(ds.Tables[0].Rows[0]["ValidFrom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["VailTo"].ToString() != "")
                {
                    model.VailTo = DateTime.Parse(ds.Tables[0].Rows[0]["VailTo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DateAdded"].ToString() != "")
                {
                    model.DateAdded = DateTime.Parse(ds.Tables[0].Rows[0]["DateAdded"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastModified"].ToString() != "")
                {
                    model.LastModified = Convert.ToDateTime(ds.Tables[0].Rows[0]["LastModified"].ToString());
                    //model.LastModified = DateTime.Parse(ds.Tables[0].Rows[0]["LastModified"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Ordered"].ToString() != "")
                {
                    model.Ordered = int.Parse(ds.Tables[0].Rows[0]["Ordered"].ToString());
                }
                #region 调试错误
                //if (ds.Tables[0].Rows[0]["Weight"].ToString() != "")
                //{
                //    model.Weight = decimal.Parse(ds.Tables[0].Rows[0]["Weight"].ToString());
                //}
                if (ds.Tables[0].Rows[0]["Weight"].ToString() != "")
                {
                    //try
                    //{
                        model.Weight = Decimal.Parse(ds.Tables[0].Rows[0]["Weight"].ToString(), NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
                    //}
                    //catch 
                    //{
                    //    throw new Exception("[Weight]值为：" + ds.Tables[0].Rows[0]["Weight"] + "-----ItCode:" + ds.Tables[0].Rows[0]["ItemCode"] + "---");  
                    //}
                }
                #endregion
                //添加促销时间、技术文件名、备货时效

                if (ds.Tables[0].Rows[0]["PromoteFrom"].ToString() != "")
                {
                    model.PromoteFrom = DateTime.Parse(ds.Tables[0].Rows[0]["PromoteFrom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PromoteTo"].ToString() != "")
                {
                    model.PromoteTo = DateTime.Parse(ds.Tables[0].Rows[0]["PromoteTo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IntroDocFiles"].ToString() != "")
                {
                    model.IntroDocFiles = ds.Tables[0].Rows[0]["IntroDocFiles"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ShipDays"].ToString() != "")
                {
                    model.ShipDays = int.Parse(ds.Tables[0].Rows[0]["ShipDays"].ToString());
                }
                model.WeightUnit = ds.Tables[0].Rows[0]["WeightUnit"].ToString();
                if (ds.Tables[0].Rows[0]["Freeze"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Freeze"].ToString() == "1") || (ds.Tables[0].Rows[0]["Freeze"].ToString().ToLower() == "true"))
                    {
                        model.Freeze = true;
                    }
                    else
                    {
                        model.Freeze = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["FreezeFrom"].ToString() != "")
                {
                    model.FreezeFrom = DateTime.Parse(ds.Tables[0].Rows[0]["FreezeFrom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FreezeTo"].ToString() != "")
                {
                    model.FreezeTo = DateTime.Parse(ds.Tables[0].Rows[0]["FreezeTo"].ToString());
                }

                if (ds.Tables[0].Rows[0]["MinNumber"].ToString() != "")
                {
                    model.MinNumber = int.Parse(ds.Tables[0].Rows[0]["MinNumber"].ToString());
                }

                if (ds.Tables[0].Rows[0]["MinMultiple"].ToString() != "")
                {
                    model.MinMultiple = int.Parse(ds.Tables[0].Rows[0]["MinMultiple"].ToString());
                }

                if (ds.Tables[0].Rows[0]["IsRecommend"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsRecommend"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsRecommend"].ToString().ToLower() == "true"))
                    {
                        model.IsRecommend = true;
                    }
                    else
                    {
                        model.IsRecommend = false;
                    }
                }

                return model;
            }
            else
            {
                return null;
            }
        }




        /// <summary>
        /// 获取产品attribute的数据和相应的值
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        public DataSet GetAttributeWithValueByProduct(string productID)
        {
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@ProductID", SqlDbType.Int)
                   
                    };
            parameters[0].Value = productID;
            return DbHelperSQL.RunProcedure("UP_Attribute_GetByProduct", parameters, "ds");
        }

        /// <summary>
        /// 根据产品获取分量计价信息
        /// </summary>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        public DataSet GetPDiffPriceByProductCode(string itemCode)
        {
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@ItemCode", SqlDbType.VarChar,20)
                   
                    };
            parameters[0].Value = itemCode;

            return DbHelperSQL.RunProcedure("UP_PDiffPrice_GetList", parameters, "ds");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ProductID,ItemCode,ItemClass,CategoryID,InvQty,BookedQty,Image,Currency,BasicPrice,IsPromote,PromoteDiscount,PromoteQty,TaxClassID,TaxRate,BrandID,BaseUnit,SalesUnit,Vaild,ValidFrom,VailTo,DateAdded,LastModified,Ordered ");
            strSql.Append(" FROM Product ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, int LanguageID, int BrandID, int CategoryID, string KeyWords, string ProductIDWhere, bool ShowFreeze,bool  IsDisplay,int IndustryID)
        {
            return GetList(PageSize, PageIndex, LanguageID, BrandID, CategoryID, KeyWords, ProductIDWhere, "", ShowFreeze, IsDisplay, IndustryID);
        }




        public DataSet GetList(int PageSize, int PageIndex, int LanguageID, int BrandID, int CategoryID, string KeyWords, string ProductIDWhere, string ProductDescript, bool ShowFreeze, bool IsDisplay, int IndustryID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@OrderfldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                     new SqlParameter("@OrderType", SqlDbType.Bit),           
                     new SqlParameter("@LanguageID", SqlDbType.Int),
                      new SqlParameter("@BrandID", SqlDbType.Int),
                      new SqlParameter("@CategoryID", SqlDbType.Int),
                      new SqlParameter("@IsReCount", SqlDbType.Bit),
                      new SqlParameter("@IsPromote", SqlDbType.Int),  
                     new SqlParameter("@KeyWords", SqlDbType.VarChar, 100),
                     new SqlParameter("@ProductIDWhere",SqlDbType.VarChar,4000),
                     new SqlParameter("@ProductDescript",SqlDbType.VarChar,2000),
                     new SqlParameter("@ShowFreeze",SqlDbType.Bit),
                     new SqlParameter("@IsDisplay",SqlDbType.Bit),
                     new SqlParameter("@IndustryID",SqlDbType.Int)
                    };
            parameters[0].Value = "a.ProductID";
            parameters[1].Value = PageSize;
            parameters[2].Value = PageIndex;
            parameters[3].Value = 0;
            parameters[4].Value = LanguageID;
            parameters[5].Value = BrandID;
            parameters[6].Value = CategoryID;
            parameters[7].Value = 1;
            parameters[8].Value = -1;
            parameters[9].Value = KeyWords;
            parameters[10].Value = ProductIDWhere;
            parameters[11].Value = ProductDescript;
            parameters[12].Value = ShowFreeze;
            parameters[13].Value = IsDisplay;
            parameters[14].Value = IndustryID;                                                
            return DbHelperSQL.RunProcedure("UP_Product_GetPageList", parameters, "ds");
        }
        /// <summary>
        /// 根据code得到产品
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public DataTable getProByCode(string Code)
        {
            string sql = "select ProductID,ItemCode,ItemClass,BasicPrice,[Weight],MinMultiple,MinNumber,TaxRate from Product where  ItemCode='{0}'";
            DataSet dt = DbHelperSQL.GetDataSet(string.Format(sql, Code));
            if (dt.Tables.Count > 0)
            {
                return dt.Tables[0];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 模糊查产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable getProList(string id)
        {
            string sql = "select top 15 ProductID,ItemCode,ItemClass,BasicPrice,[Weight],MinMultiple,MinNumber,TaxRate from Product where  ItemCode like '{0}%'";
          DataSet dt=DbHelperSQL.GetDataSet(string.Format(sql,id));
          if (dt.Tables.Count > 0)
          {
              return dt.Tables[0];
          }
          else {
              return null;
          }
          
        }

        #endregion  成员方法
    }
}
